<?
include('wallet_db.html');
$params[sid] = $sid;
$params[action] = "editContacts";
$url = "$baseURL/$webDIR/?".encode_params($params);
#$store_name = get_store_name($id);
if (isset($_POST['form_sent']) && $_POST['form_sent'] == 'yes') {
	foreach ($_POST as $key => $value) {
		$_POST[$key] = cleanup_input_text($value);
	}
	pg_query($dbh81,"BEGIN");
	$query = "DELETE FROM contacts WHERE reseller_id='$id'";
	$res = pg_query($dbh81,$query);
	if (!empty($_POST['email_na'])) {
 		$query = "INSERT INTO contacts VALUES ('$id','email','".$_POST['email_na']."','".$_POST['email_comment']."')";
 		$res = pg_query($dbh81,$query);
	}
	if (!empty($_POST['icq_na'])) {
 		$query = "INSERT INTO contacts VALUES ('$id','icq','".$_POST['icq_na']."','".$_POST['icq_comment']."')";
 		$res = pg_query($dbh81,$query);
	}
	if (!empty($_POST['phone_na'])) {
 		$query = "INSERT INTO contacts VALUES ('$id','phone','".$_POST['phone_na']."','".$_POST['phone_comment']."')";
 		$res = pg_query($dbh81,$query);
	}
	if (!empty($_POST['other_type'])) {
 		$query = "INSERT INTO contacts VALUES ('$id','".$_POST['other_type']."','".$_POST['other_na']."','".$_POST['other_comment']."')";
 		$res = pg_query($dbh81,$query);
	}
 	pg_query($dbh81,"COMMIT");
}
//------------------------------------------------------
	$sql['type']='SELECT';
	$sql['params']='*';
	$sql['table']='contacts';
	$sql['where']="reseller_id='$id'";
	$sql['options']='';
	$sql['return']='struct';
//------------------------------------------------------
$result=sql_exec($sql,$dbh81,'assoc');

foreach ($result as $value) {
	$tofill[$value['type']] = $value;
	if (!preg_match("/^email.*/",$value['type']) && !preg_match("/^phone.*/",$value['type']) && !preg_match("/^icq.*/",$value['type'])) {
		$other = $value['type'];
	}
}

?>
<table width="100%">
  <tr>
    <td colspan=2><font class="text"><b><?=translate('edit_contacts');?></b><br>
      <br>
      <?=translate('edit_contacts_desc');?>
      </font></td>
  </tr>
</table>
<br>
<br>
<TABLE border=1 class='bTable' cellspacing='0'>
  <form action='<?=$url;?>' method='POST'>
	<tr>
		<th><?=translate('contact');?></th>
		<th><?=translate('number_address');?></th>
		<th><?=translate('comment');?></th>
	</tr>
	<tr>
		<td><?=translate('email');?>:</td>
		<td><input type='text' name='email_na' value='<?=$tofill['email']['na']; ?>'></td>
		<td><input type='text' name='email_comment' value='<?=$tofill['email']['comment']; ?>'></td>
	</tr>
	<tr>
		<td><?=translate('icq');?>:</td>
		<td><input type='text' name='icq_na' value='<?=$tofill['icq']['na']; ?>'></td>
		<td><input type='text' name='icq_comment' value='<?=$tofill['icq']['comment']; ?>'></td>
	</tr>
	<tr>
		<td><?=translate('phone');?>:</td>
		<td><input type='text' name='phone_na' value='<?=$tofill['phone']['na']; ?>'></td>
		<td><input type='text' name='phone_comment' value='<?=$tofill['phone']['comment']; ?>'></td>
	</tr>
	<tr>
		<td><input type='text' name='other_type' value='<?=$tofill[$other]['type']; ?>'>:</td>
		<td><input type='text' name='other_na' value='<?=$tofill[$other]['na']; ?>'></td>
		<td><input type='text' name='other_comment' value='<?=$tofill[$other]['comment']; ?>'></td>
	</tr>
	<tr>
		<td colspan='3' align='center'><input type='submit' value='<?=translate('save');?>'>
		<input type="hidden" name="form_sent" value="yes"></td>
	</tr>

</form>
</TABLE>


